package com.hjj.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hjj.springboot.entity.*;
import com.hjj.springboot.service.ITalkDetailService;
import com.hjj.springboot.service.ITalkImageService;
import com.hjj.springboot.service.ITalkService;
import com.hjj.springboot.service.IWxUserInfoService;
import com.hjj.springboot.util.DateUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.*;

@RestController
@RequestMapping("/talk")
public class TalkController {

    @Autowired
    private ITalkService talkService;

    @Autowired
    private ITalkDetailService talkDetailService;

    @Autowired
    private ITalkImageService talkImageService;

    @Autowired
    private IWxUserInfoService wxUserInfoService;

    @Value("${talkImagesFilePath}")
    private String talkImagesFilePath;

    @GetMapping("/list")
    public R list(){
        List<Talk> talkList=talkService.list(new QueryWrapper<Talk>().orderByDesc("id"));
        for (Talk talk:talkList){
            WxUserInfo wxUserInfo=wxUserInfoService.getOne(new QueryWrapper<WxUserInfo>().eq("openid",talk.getUid()));
            talk.setWxUserInfo(wxUserInfo);
            List<TalkImage> talkImageList=talkImageService.list(new QueryWrapper<TalkImage>().eq("mid",talk.getId()));
            talk.setTalkImagesList(talkImageList);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("talkList",talkList);
        return R.ok(map);
    }

    @GetMapping("/detail")
    public R detail(Integer id){
        List<TalkDetail> talkDetailList=talkDetailService.list(new QueryWrapper<TalkDetail>().eq("mid",id).orderByDesc("id"));
        for (TalkDetail talkDetail:talkDetailList){
           WxUserInfo wxUserInfo=wxUserInfoService.getOne(new QueryWrapper<WxUserInfo>().eq("openid",talkDetail.getUid()));
           talkDetail.setWxUserInfo(wxUserInfo);
        }
        Talk talk=talkService.getById(id);
        talk.setTalkImagesList(talkImageService.list(new QueryWrapper<TalkImage>().eq("mid",id)));
        talk.setWxUserInfo(wxUserInfoService.getOne(new QueryWrapper<WxUserInfo>().eq("openid",talk.getUid())));
        talk.setTalkDetailsList(talkDetailList);
        HashMap<String, Object> map = new HashMap<>();
        map.put("talk",talk);
        return R.ok(map);
    }

    @GetMapping("/deleteTalk")
    public R deleteTalk(Integer id){
        System.out.println(id);
        talkService.removeById(id);
        talkDetailService.remove(new QueryWrapper<TalkDetail>().eq("mid",id));
        talkImageService.remove(new QueryWrapper<TalkImage>().eq("mid",id));
        return R.ok();
    }

    @GetMapping("/deleteTalkDetail")
    public R deleteTalkDetail(Integer id){
        System.out.println(id);
        talkDetailService.removeById(id);
        return R.ok();
    }

    //    上传图片
    @RequestMapping("/uploadImage")
    public Map<String,Object> uploadImage(@RequestParam("talkImage") MultipartFile file) throws Exception {
        Map<String,Object> reultMap=new HashMap<>();
        if(!file.isEmpty()){
            String originalFilename= file.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName= DateUtil.getCurrentDateStr()+suffix;
            FileUtils.copyInputStreamToFile(file.getInputStream(),new File(talkImagesFilePath+newFileName));
            reultMap.put("code",0);
            reultMap.put("msg","上传成功");
            Map<String,Object> dataMap=new HashMap<>();
            dataMap.put("title",newFileName);
            dataMap.put("src","img/talkImgs/"+newFileName);
            reultMap.put("data",dataMap);
        }
        return reultMap;
    }

    @PostMapping("/addTalk")
    public R addTalk(@RequestBody Talk talk){
        talk.setUpTime(new Date());
        talkService.save(talk);
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",talk.getId());
        return R.ok(map);
    }

    @PostMapping("/addTalkImage")
    public R addTalkImage(@RequestBody List<TalkImage> talkImageList){
        for (TalkImage talkImage:talkImageList){
            talkImageService.save(talkImage);
        }
        System.out.println(talkImageList);
        return R.ok();
    }

    @GetMapping("/addTalkDetail")
    public R addTalkDetail(TalkDetail talkDetail){
        talkDetail.setUpTime(new Date());
        talkDetailService.save(talkDetail);
        return R.ok();
    }

}
